<?php
/** class.blogbase.php
 * 
 * Created our base objects that all classes will need to use
 * 
 * @author Ted, tass2001-at-gmail-dot-com
 * @package phpsimpleblog
 * @version $Id: class.blogbase.php 13 2013-03-31 02:20:53Z tass2001@gmail.com $
 * @website https://code.google.com/p/phpsimpleblog/
 */
//Define DB table name constants
define('ENTRY_TABLE'   ,'entries');
define('COMMENT_TABLE' ,'comments');
define('USERS_TABLE'   ,'users');
class BlogBase{
    //Since we want to share these variables across classes we must initate them privately and statically
    public static $config = false;
    public static $db	  = false;
    
    /* Construct our base object using internal variables
    */
    public function __construct(){
	if(!self::$config)
	    self::$config = $this->__load_config();
	$this->config = self::$config;
	if(!self::$db)
	   self::$db = new mysqli('localhost',$this->config->db_user,$this->config->db_password,$this->config->db_name);
	$this->db = self::$db;
    }
    
    /* Load our XML config file into an object so that our extending classes have access to it
     * @param	String $config_loc	The default location of the XML file - Override at your own peril.
     * @return	Object $return		An object of our configuration options with values.
     */
    private static function __load_config($config_loc=false){
	$config_loc = ($config_loc) ? $config_loc : DOC_ROOT.'/includes/config.xml';
	$config_data = new stdClass();
	if(!file_exists($config_loc))
	    throw new Exception('Missing config file - Quitting!');
	$config = simplexml_load_file($config_loc);
	foreach($config as $tag=>$data){
	    if(count($data) > 0){
		foreach($data as $child_tag=>$child_data)
		    $config_data->$tag->$child_tag = (string)$child_data;
	    }else{
		$config_data->$tag = (string)$data;
	    }
	}
        return $config_data;
    }
    
    /* A simple wrapper for mysqli_real_escape_string because I'm lazy and it'll make the code hella nicer
     * @param  String $str	Something to escape
     * @return String 		Something escaped
     */
    public function esc($str,$xss_esc=false){
	if($xss_esc)
	    $str = strip_tags($str);
	return $this->db->real_escape_string(htmlspecialchars($str));
    }
    
    /* Parse a URI into an array so that we know what functions the user is looking to access
     * @param  String $str		A URI
     * @return Array  $return_data	An array of the URI
     */
    public static function uriParse($str){
        $return_data = array();
        foreach(explode('/',$str) as $item){
            if($item)
                $return_data[] = strtolower(preg_replace('/\s{1,}/','-',$item));
        }
        return $return_data;
    }
    
    /* Upload a given file to the uploads folder.
     * @return String 	JSON encoded array of the success/location of the uploaded file.
     */
    public function upload(){
	$return_state = array('success'=>false);
	if(AUTH && $_SERVER['HTTP_X_FILE_SIZE'] <= 3145728 && isset($_SERVER['HTTP_X_FILE_NAME'])){
	    $return_state['location'] = '/uploads/'.substr(trim($_SERVER['HTTP_X_FILE_NAME']),0,50);
	    file_put_contents(DOC_ROOT.$return_state['location'],file_get_contents('php://input'));
	    $return_state['success'] = true;
	}
	return json_encode($return_state);
    }
}
?>